package com.example.peanutsubway.dao.impl;

import com.example.peanutsubway.dao.RoleInfoDao;
import com.example.peanutsubway.entity.RoleInfo;
import com.example.peanutsubway.util.JDBCUtils;
import com.example.peanutsubway.util.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Connection;
import java.util.List;

/**
 * @Author：zzt
 * @Version：1.0
 * @Date：2021/10/17-14:12
 * @Since:jdk1.8
 * @Description:
 */

public class RoleInfoDaoImpl implements RoleInfoDao {
    private Connection connection;

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    @Override
    public Boolean addRoleInfo(RoleInfo roleInfo) {
        String sql = "INSERT INTO pn_role (rid,rolename,state) VALUES (?,?,?)";
        return JDBCUtils.update(connection,sql,roleInfo.getRid(),roleInfo.getRolename(),roleInfo.getState())>0;
    }

    @Override
    public List<RoleInfo> findAllRoleInfo(PageInfo pageInfo) {
        String sql = "select * from pn_role limit ?,?";
        return JDBCUtils.find(sql,RoleInfo.class,pageInfo.getStartIndex(),pageInfo.getPageNum());
    }

    @Override
    public Integer count() {
        String sql = "select count(1) from pn_role";
        return JDBCUtils.count(sql).intValue();
    }

    @Override
    public Boolean deleteRole(Integer id) {
        String sql = "DELETE FROM pn_role WHERE rid = ?";
        return JDBCUtils.update(sql,id)>0;
    }

    @Override
    public Boolean updateRole(RoleInfo roleInfo) {
        String sql = "UPDATE pn_role SET rolename = ?,state = ? where rid = ?";
        return JDBCUtils.update(sql,roleInfo.getRolename(),roleInfo.getState(),roleInfo.getRid())>0;
    }
}
